import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getData, postSetting } from '@/services';
export interface IndexModelState {
    name: string;
}

export interface IndexModelType {
    namespace: 'setting';
    state: IndexModelState;
    effects: {
        query: Effect; visible: Effect
    };
    reducers: {
        // save: Reducer<IndexModelState>;
        // 启用 immer 之后
        save: ImmerReducer<IndexModelState>;
    };
    subscriptions?: { setup: Subscription };
}

const ViewModel: IndexModelType = {
    namespace: 'setting',

    state: {
        name: '',
    },

    effects: {
        // 系统设置
        *query({ payload }, { call, put }) {
            let res = yield call(postSetting, `api/setting/get`)
            const { adminSystemUrl, baiduAnalyticsId, googleAnalyticsId, i18n, seoDesc, seoKeyword, systemFavicon, systemFooterInfo, systemLogo, systemTitle, systemUrl } = res.data
            yield put({
                type: "save",
                payload: {
                    adminSystemUrl, baiduAnalyticsId, googleAnalyticsId, i18n, seoDesc, seoKeyword, systemFavicon, systemFooterInfo, systemLogo, systemTitle, systemUrl
                }
            })
        },

        *visible({ payload }, { call, put }) {
            let res = yield call(getData, `api/file?page=${payload.page}&pageSize=${payload.size}`)
            yield put({
                type: "save",
                payload: {
                    visibleList: res.data[0],
                    visibleLength: res.data[1]
                }
            })
        },

    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };

        },
        // 启用 immer 之后
        // save(state, action) {
        //   state.name = action.payload;
        // },
    },
    // subscriptions: {
    //     setup({ dispatch, history }) {
    //         return history.listen(({ pathname }) => {
    //             if (pathname === '/') {
    //                 dispatch({
    //                     type: 'query',
    //                 });
    //             }
    //         });
    //     },
    // },
};

export default ViewModel;






